using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour 
{
	public int startingLives = 0;
	
	public Transform playerSpawnPoint = null;
	public GameObject playerPrefab = null;
	public GameObject gameOverScreen = null;
	
	public GUIText kills = null;
	public GUIText score = null;
	public GUIText lives = null;
	
	private static int _currentKills = 0;
	private static int _currentLives = 0;
	private static int _currentScore = 0;
	
	private static bool _isPlayerSpawned = false;
	
	public static void EnemyKilled(int score)
	{
		//Increment lives
		_currentKills++;
		
		//Add a set amount of score
		_currentScore+= score;
	}
	
	public static void PlayerDies()
	{
		//Incrementing lives
		_currentLives --;
		
		//Set the _isPlayerSpawned to false
		_isPlayerSpawned = false;
	}
	
	private void _ProcessLives()
	{
		//Check wether we should spawn the player;
		if(_isPlayerSpawned == false && _currentLives >= 0)
		{
			//Spawn the player
			Instantiate(playerPrefab, playerSpawnPoint.position, Quaternion.identity);
			
			//Tell the code that the player has been spawned
			_isPlayerSpawned = true;
		}
		else if(_currentLives < 0) //If we have no lives
		{
			//Enable the game over screen
			gameOverScreen.SetActive(true);
		}
	}
	
	// Use this for initialization
	void Start () 
	{
		//Set the starting lives
		_currentLives = startingLives;
	}
	
	// Update is called once per frame
	void Update () 
	{
		//Process the lives
		_ProcessLives();
		
		//Update the text
		kills.text = "Kills: " + _currentKills.ToString();
		score.text = "Score: " + _currentScore.ToString();
		lives.text = "Lives: " + Mathf.Clamp(_currentLives, 0, int.MaxValue).ToString();
	}
}
